package general.servlet;

import com.alibaba.fastjson.JSONObject;
import general.servlet.util.IOUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @desc 描述
 * create on 2018/10/25 by watson
 */
@WebServlet("/servlet")
public class ServletDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置网页响应类型
        response.setContentType("application/json");
        //实现具体操作
        PrintWriter out = response.getWriter();

        Map<String, String[]> parameterMap = request.getParameterMap();

        String add = request.getParameter("add");
        String ioStr = IOUtils.getString(request);
        if (StringUtils.isBlank(ioStr)){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",400);
            jsonObject.put("msg","参数不能为空");

            out.println(jsonObject.toJSONString());
        } else {
            JSONObject ioJson = JSONObject.parseObject(ioStr);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",200);
            jsonObject.put("msg","成功");
            jsonObject.put("data",ioJson.getString("add"));

//            out.println("123888");
            out.println(jsonObject.toJSONString());
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
